//
//  GYUserInfoViewController.m
//  research
//
//  Created by 章鱼 on 17/3/29.
//  Copyright © 2017年 simon. All rights reserved.
//

#import "GYUserInfoViewController.h"
#import "GYUserInfoCell.h"
#import "GYNicknameViewController.h"
#import "GYConfigDBManager.h"

@interface GYUserInfoViewController ()<
UITableViewDataSource,
UITableViewDelegate
>

@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) UIView *sexSelectedView;
@property (nonatomic, strong) UIView *backView;
@property (nonatomic, strong) UILabel *infoLable;
@property (nonatomic, strong) UILabel *manLable;
@property (nonatomic, strong) UIView *lineView;
@property (nonatomic, strong) UILabel *womanLable;

@property (nonatomic, strong) UIButton *manButton;
@property (nonatomic, strong) UIButton *womanButton;
@property (nonatomic, strong) NSString *sextType;

@end

@implementation GYUserInfoViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self basicSetup];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [self addBackButtonItemWithTitle:@"返回" backImg:@"navigation-bar_back_normal@2x"];
    [self.tableView reloadData];
}

- (void)basicSetup {
    [self initNavBarWithTitle:@"个人信息" tintColor:[UIColor orz_mainColor]];
    [self.navBar setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:
                                         [UIColor whiteColor],NSForegroundColorAttributeName,
                                         [UIFont boldSystemFontOfSize:18],
                                         NSFontAttributeName,
                                         nil]];
    [self.view setBackgroundColor:[UIColor orz_globalBackground]];
}



#pragma mark - UI
- (UITableView *)tableView {
    
    if (!_tableView) {
        _tableView = [[UITableView alloc]initWithFrame:CGRectZero style:UITableViewStylePlain];
        _tableView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
        _tableView.backgroundColor = [UIColor orz_globalBackground];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.scrollEnabled = NO;
        _tableView.layer.borderColor = [UIColor orz_line].CGColor;
        _tableView.layer.borderWidth = 1.f;
        [self.view addSubview:_tableView];
        [_tableView setSeparatorStyle:UITableViewCellSeparatorStyleNone];
        [self.tableView registerClass:[GYUserInfoCell class] forCellReuseIdentifier:@"userinfocell"];
        [self initUserViewUI];
        
    }
    return _tableView;
}


- (void)initUserViewUI {
 
    [_tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(64.f);
        make.right.mas_equalTo(self.view);
        make.left.mas_equalTo(self.view);
        make.bottom.mas_equalTo(self.view);
    }];
}

#pragma mark - UITableViewDataSource

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    
    return 2;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if (section == 0) {
        return 3;
    }
    return 2;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    GYUserInfoCell *cell = [tableView dequeueReusableCellWithIdentifier:@"userinfocell" forIndexPath:indexPath];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    if (indexPath.section == 0) {
        if (indexPath.row == 0) {
            cell.titleLable.text = @"昵称";
            cell.infoLable.text = [[GYConfigDBManager defaultManager] userInformation].nickname;
        }
        if (indexPath.row == 1) {
            cell.titleLable.text = @"性别";
            if ([[[GYConfigDBManager defaultManager] userInformation].gender isEqualToString:@"1"]) {
                
                cell.infoLable.text = @"女";
            }else{
                cell.infoLable.text = @"男";
            }
       
        }
        if (indexPath.row == 2) {
            cell.titleLable.text = @"地区";
            cell.infoLable.text = [[GYConfigDBManager defaultManager] userInformation].address;

            cell.lineView.hidden = YES;
        }
    }
    if (indexPath.section == 1) {
        if (indexPath.row == 0) {
            cell.titleLable.text = @"手机";
            cell.infoLable.text = [[GYConfigDBManager defaultManager] userInformation].phone;

        }
        if (indexPath.row == 1) {
            cell.titleLable.text = @"邮箱";
            cell.infoLable.text = [[GYConfigDBManager defaultManager] userInformation].email;
            cell.lineView.hidden = YES;

        }
    }
    return cell;
}

#pragma mark - tableViewcell点击
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 0) {
        if (indexPath.row == 0) {
            GYNicknameViewController *userCtrl = [[GYNicknameViewController alloc] init];
            userCtrl.hidesBottomBarWhenPushed = YES;
            userCtrl.userName = [[GYConfigDBManager defaultManager] userInformation].nickname;
            userCtrl.childViewTitle = @"修改昵称";
            [self.navigationController pushViewController:userCtrl animated:YES];
            
        }else if(indexPath.row == 1) {
            
            [self sexSelectedView];
        }
        else if(indexPath.row == 2) {
            GYNicknameViewController *userCtrl = [[GYNicknameViewController alloc] init];
            userCtrl.hidesBottomBarWhenPushed = YES;
            userCtrl.userName = [[GYConfigDBManager defaultManager] userInformation].nickname;
            userCtrl.childViewTitle = @"修改地址";

            [self.navigationController pushViewController:userCtrl animated:YES];
        }

    }
    if (indexPath.section == 1) {
        if (indexPath.row == 0) {
            GYNicknameViewController *userCtrl = [[GYNicknameViewController alloc] init];
            userCtrl.hidesBottomBarWhenPushed = YES;
            userCtrl.userName = [[GYConfigDBManager defaultManager] userInformation].nickname;
            userCtrl.childViewTitle = @"修改电话";

            [self.navigationController pushViewController:userCtrl animated:YES];
            
        }else if(indexPath.row == 1) {
            GYNicknameViewController *userCtrl = [[GYNicknameViewController alloc] init];
            userCtrl.hidesBottomBarWhenPushed = YES;
            userCtrl.userName = [[GYConfigDBManager defaultManager] userInformation].nickname;
            userCtrl.childViewTitle = @"修改邮箱";

            [self.navigationController pushViewController:userCtrl animated:YES];
        }
    }
}


-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    if (section==0) {
        return 30;
    }else{
        return 20;
    }
}
-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 0;
}

- (UIView *)sexSelectedView {
    _sexSelectedView = [[UIView alloc] init];
    [self.view addSubview:_sexSelectedView];
    _sexSelectedView.backgroundColor = [UIColor colorWithHex:@"000000" alpha:0.13];
    
    _backView = [[UIView alloc] init];
    [_sexSelectedView addSubview:_backView];
    _backView.backgroundColor = [UIColor whiteColor];
    _backView.layer.cornerRadius = 4.f;
    _backView.layer.masksToBounds = YES;
    
    _infoLable = [[UILabel alloc] init];
    [_backView addSubview:_infoLable];
    _infoLable.text = @"性别";
    _infoLable.textColor = [UIColor colorWithHex:@"323232" alpha:1];
    _infoLable.font = [UIFont systemFontOfSize:13];
    
    _manLable = [[UILabel alloc] init];
    [_backView addSubview:_manLable];
    _manLable.text = @"男士";
    _manLable.textColor = [UIColor colorWithHex:@"323232" alpha:1];
    _manLable.font = [UIFont systemFontOfSize:12];
    
    _lineView = [[UIView alloc] init];
    [_backView addSubview:_lineView];
    _lineView.backgroundColor = [UIColor orz_line];
    
    
    _womanLable = [[UILabel alloc] init];
    [_backView addSubview:_womanLable];
    _womanLable.text = @"女士";
    _womanLable.textColor = [UIColor colorWithHex:@"323232" alpha:1];
    _womanLable.font = [UIFont systemFontOfSize:12];
    
    _manButton = [[UIButton alloc] init];
    [_backView addSubview:_manButton];
    [_manButton setImage:[UIImage imageNamed:@"popovers_button_normal"] forState:UIControlStateNormal];
    [_manButton setImage:[UIImage imageNamed:@"popovers_button_press"] forState:UIControlStateSelected];
    [_manButton addTarget:self action:@selector(manClick:) forControlEvents:UIControlEventTouchUpInside];
    
    _womanButton = [[UIButton alloc] init];
    [_backView addSubview:_womanButton];
    [_womanButton setImage:[UIImage imageNamed:@"popovers_button_normal"] forState:UIControlStateNormal];
    [_womanButton setImage:[UIImage imageNamed:@"popovers_button_press"] forState:UIControlStateSelected];
    [_womanButton addTarget:self action:@selector(womanClick:) forControlEvents:UIControlEventTouchUpInside];

    if ([[[GYConfigDBManager defaultManager] userInformation].gender isEqualToString:@"0"]) {
        _manButton.selected = YES;
        _womanButton.selected = NO;
    }else {
        _womanButton.selected = YES;
        _manButton.selected = NO;

    }
    [self setSexSelectedViewUI];
    return _sexSelectedView;
}

- (void)setSexSelectedViewUI {
    
    [_sexSelectedView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self.view);
    }];
    
    [_backView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(164.f);
        make.left.mas_equalTo(50.f);
        make.right.mas_equalTo(-50.f);
        make.height.mas_equalTo(160.f);
    }];
    
    [_infoLable mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(20.f);
        make.left.mas_equalTo(20.f);
        
    }];
    
    [_manLable mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(_infoLable.mas_bottom).offset(31.f);
        make.left.mas_equalTo(32.f);
        
    }];
    
    [_lineView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(_manLable.mas_bottom).offset(10.f);
        make.left.mas_equalTo(20.f);
        make.right.mas_equalTo(-20.f);
        make.height.mas_equalTo(1.f);
    }];
    
    [_womanLable mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(_lineView.mas_bottom).offset(10.f);
        make.left.mas_equalTo(32.f);
        
    }];
    
    [_manButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(-34.f);
        make.centerY.mas_equalTo(_manLable.mas_centerY);
        make.size.mas_equalTo(CGSizeMake(14, 14));
    }];
    
    [_womanButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(-34.f);
        make.centerY.mas_equalTo(_womanLable.mas_centerY);
        make.size.mas_equalTo(CGSizeMake(14, 14));
    }];
    
}

/**
 *  重写touchesEnded方法
 */
- (void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    if ([touches anyObject].view != _backView) {
        // 判断点击的区域如果不是菜单按钮_btnMenu, 则关闭菜单
        [self dismissSlectedView];
    }
}
- (void)dismissSlectedView {
    if (_sexSelectedView) {
        [_sexSelectedView removeFromSuperview];
        
    }
}

- (void)manClick:(UIButton *)btn {
    _sextType = @"0";
    [self modifyUserInfo:_sextType];

}

- (void)womanClick:(UIButton *)btn {
    _sextType = @"1";
    [self modifyUserInfo:_sextType];

}

- (void)modifyUserInfo:(NSString *)type{

    [[YGHttpManager defaultManager] fetchGetUserSettingWithtoken:[[GYConfigDBManager defaultManager] gyToken] userId:[NSString stringWithFormat:@"%ld",(long)[[GYConfigDBManager defaultManager] userInformation].id] nickname:[[GYConfigDBManager defaultManager] userInformation].nickname phone:[[GYConfigDBManager defaultManager] userInformation].phone email:[[GYConfigDBManager defaultManager] userInformation].email address:[[GYConfigDBManager defaultManager] userInformation].address gender:type success:^(id responseObj) {
        GYCommonModel *commonmodel = [GYCommonModel mj_objectWithKeyValues:responseObj];
        if (commonmodel.status == 1) {
            
            [VBAlertView showTextHUD:@"修改成功"];
            GYUserInfoModel * model = [GYUserInfoModel mj_objectWithKeyValues:commonmodel.data];
            [[GYConfigDBManager defaultManager] setUserInformation:model];
            [self dismissSlectedView];
            [self.tableView reloadData];
        }else{
            [VBAlertView showTextHUD:commonmodel.message];
            [self dismissSlectedView];
        }
    } failure:^(NSError *error) {
        
    }];
}



@end
